/*
 * JThumbnailPanel.java
 *
 *  Copyright (C) 2007 COMP5425 Multimedia Storage, Retrieval and Delivery
 *  The School of Information Technology
 *  The University of Sydney
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */
package usyd.comp5425.ui.imageviewer;

import com.sun.jaf.ui.ActionManager;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;

/**
 *
 * @author  Yuezhong Zhang  SID:305275631
 */
public class JThumbnailPanel extends javax.swing.JPanel {
    public static final int THUMBSIZE_SMALL = 32;
    public static final int THUMBSIZE_MEDIUM = 64;
    public static final int THUMBSIZE_LARGE = 160;
    public JThumbnailPanel() {
        
        initComponents();
        this.setFixedCellHeight(THUMBSIZE_LARGE);
        this.setFixedCellWidth(THUMBSIZE_LARGE + 10);
        initAction();
        this.imageList.setModel(new ImageListModel());
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        javax.swing.JScrollPane jScrollPane1;
        javax.swing.JPanel pagePanel;

        jScrollPane1 = new javax.swing.JScrollPane();
        imageList = new javax.swing.JList();
        pagePanel = new javax.swing.JPanel();
        pageUpBtn = new javax.swing.JButton();
        pageDownBtn = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        imageList.setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
        imageList.setCellRenderer(cellRenderer);
        imageList.setDoubleBuffered(true);
        imageList.setLayoutOrientation(javax.swing.JList.HORIZONTAL_WRAP);
        imageList.setVisibleRowCount(0);
        imageList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                imageListMouseClicked(evt);
            }
        });

        jScrollPane1.setViewportView(imageList);

        add(jScrollPane1, java.awt.BorderLayout.CENTER);

        pagePanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        pageUpBtn.setText("Previous");
        pageUpBtn.setEnabled(false);
        pagePanel.add(pageUpBtn);

        pageDownBtn.setText("Next");
        pageDownBtn.setEnabled(false);
        pagePanel.add(pageDownBtn);

        add(pagePanel, java.awt.BorderLayout.SOUTH);

    }// </editor-fold>//GEN-END:initComponents
    
    private void imageListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_imageListMouseClicked
// TODO add your handling code here:
        // if right mouse button clicked (or me.isPopupTrigger())
        if (SwingUtilities.isRightMouseButton(evt) && !imageList.isSelectionEmpty() && imageList.locationToIndex(evt.getPoint()) == imageList.getSelectedIndex()) {
            if(popupMenu !=null)
                popupMenu.show(imageList, evt.getX(), evt.getY());
        }
        
    }//GEN-LAST:event_imageListMouseClicked
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList imageList;
    private javax.swing.JButton pageDownBtn;
    private javax.swing.JButton pageUpBtn;
    // End of variables declaration//GEN-END:variables
    private ResultListCellRenderer  cellRenderer = new ResultListCellRenderer();
    /**
     * Getter for property setModel.
     * @return Value of property setModel.
     */
    public ImageListModel getListModel() {
        return (ImageListModel)this.imageList.getModel();
    }
    
    /**
     * Setter for property setModel.
     * @param setModel New value of property setModel.
     */
    public void setListModel(ImageListModel model) {
        this.imageList.setModel(model);
        changePageButtonState();
    }
    
    
    /**
     * Getter for property fixedCellHeight.
     * @return Value of property fixedCellHeight.
     */
    public int getFixedCellHeight() {
        return this.getImageList().getFixedCellHeight();
    }
    
    /**
     * Setter for property fixedCellHeight.
     * @param fixedCellHeight New value of property fixedCellHeight.
     */
    public void setFixedCellHeight(int fixedCellHeight) {
        this.getImageList().setFixedCellHeight(fixedCellHeight);
        
    }
    
    
    public int getFixedCellWidth() {
        return this.getImageList().getFixedCellWidth();
    }
    
    /**
     * Setter for property fixedCellWidth.
     * @param fixedCellWidth New value of property fixedCellWidth.
     */
    public void setFixedCellWidth(int fixedCellWidth) {
        this.getImageList().setFixedCellWidth(fixedCellWidth);
    }
    
    public javax.swing.JList getImageList() {
        return imageList;
    }
    
    public void setImageList(javax.swing.JList imageList) {
        this.imageList = imageList;
    }
    public void setRankingEnabled(boolean b){
        cellRenderer.setRankingEnabled(b);
        this.imageList.repaint();
    }
    
    private JPopupMenu popupMenu;
    public void setPopupMenu(JPopupMenu menu){
        this.popupMenu = menu;
    }
    private void initAction(){
        pageUpBtn.setAction(ActionManager.getInstance().getAction("pageup-command"));
        pageDownBtn.setAction(ActionManager.getInstance().getAction("pagedown-command"));
    }

    public void changePageButtonState() {
        pageUpBtn.setEnabled(getListModel().canPageUp());
        pageDownBtn.setEnabled(getListModel().canPageDown());
    }
}
